package cn.shengchao.examstar.product.acl.adapters.repositories;

import cn.hutool.core.collection.CollUtil;
import cn.shengchao.examstar.product.acl.ports.repositories.IProductUserOrderAfterLogRepository;
import cn.shengchao.examstar.product.infrastructure.dao.TProductUserOrderAfterLogDao;
import cn.shengchao.examstar.product.infrastructure.dao.po.TProductUserOrderAfterLog;
import cn.shengchao.examstar.rpc.orders.res.PayOrderInfoDto;
import cn.shengchao.examstar.rpc.orders.res.PayOrderItemInfoDto;
import cn.shengchao.examstar.rpc.product.base.ProductPropertyDto;
import cn.shengchao.examstar.rpc.product.base.ProductSettingDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @program: examstarapi
 * @description: 订单产品后续处理
 * @author: Xiong weiqiang
 * @create: 2025-06-30 16:28
 */
@Service
public class ProductUserOrderAfterLogRepository implements IProductUserOrderAfterLogRepository {
    @Autowired
    TProductUserOrderAfterLogDao tProductUserOrderAfterLogDao;
    @Override
    public void logError(String userId, PayOrderInfoDto payOrderInfoDto, PayOrderItemInfoDto orderItemInfoDto, String sysLog) {
        TProductUserOrderAfterLog tProductUserOrderAfterLog=new TProductUserOrderAfterLog();
        if (orderItemInfoDto==null){
            tProductUserOrderAfterLog.setSubProductId(Long.valueOf(payOrderInfoDto.getProductId()));
            tProductUserOrderAfterLog.setSubProductType(payOrderInfoDto.getProductType());
        }else {
            tProductUserOrderAfterLog.setSubProductId(Long.valueOf(orderItemInfoDto.getProductId()));
            tProductUserOrderAfterLog.setSubProductType(payOrderInfoDto.getProductType());
        }
        tProductUserOrderAfterLog.setHasSubProduct(CollUtil.isEmpty(payOrderInfoDto.getOrderItemInfos())?0:1);
        tProductUserOrderAfterLog.setOrderTime(payOrderInfoDto.getOrderTime());
        tProductUserOrderAfterLog.setOrderId(payOrderInfoDto.getOrderId());
        tProductUserOrderAfterLog.setUserId(Long.parseLong(userId));
        tProductUserOrderAfterLog.setSysLog(sysLog);
        tProductUserOrderAfterLog.setStatus(1);
        tProductUserOrderAfterLogDao.insert(tProductUserOrderAfterLog);
    }

    @Override
    public void logError(Long userId, String orderId, Date orderTime, ProductPropertyDto productPropertyDto,boolean isSubProduct, String s) {
        TProductUserOrderAfterLog tProductUserOrderAfterLog=new TProductUserOrderAfterLog();
        if (productPropertyDto!=null){
            ProductSettingDto productSettingDto = productPropertyDto.getCourseProductSettingDto();
            if (productPropertyDto.getCourseProductSettingDto()==null){
                tProductUserOrderAfterLog.setSubProductId(productSettingDto.getProductId());
                tProductUserOrderAfterLog.setSubProductType(productSettingDto.getProductType());
            }else {
                tProductUserOrderAfterLog.setSubProductId(productSettingDto.getProductId());
                tProductUserOrderAfterLog.setSubProductType(productSettingDto.getProductType());
            }
        }
        tProductUserOrderAfterLog.setHasSubProduct(isSubProduct?0:1);
        tProductUserOrderAfterLog.setOrderTime(orderTime);
        tProductUserOrderAfterLog.setOrderId(orderId);
        tProductUserOrderAfterLog.setUserId(userId);
        tProductUserOrderAfterLog.setSysLog(s);
        tProductUserOrderAfterLog.setStatus(1);
        tProductUserOrderAfterLogDao.insert(tProductUserOrderAfterLog);
    }
}